﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mshtml;

namespace WinApp
{
	//class="pga" - общий блок комментариев
	//class="Yp yt Xa" - корневой враппер комментария
	//class="Ub gna"> - имя комментатора
	//class="Ub TD" - имя автора/вложеного комментария
	//class="Ct" - само тело комментария
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			string path = @"D:\ProjectsOrange\2014\YouTubeCommentsExtractor\WinApp\src.htm";
			string src = System.IO.File.ReadAllText(path);
			br.DocumentText = src;
		}

		private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
		{
			Dictionary<string, Comment> comments = new Dictionary<string, Comment>();
			foreach (HtmlElement hcmt in br.Document.Body.Children[0].Children)
			{
				HTMLDivElement div = hcmt.DomElement as HTMLDivElement;
				if (div == null || div.className != "Yp yt Xa") continue;
				Comment comment = new Comment();
				comment.Id = hcmt.Id.Substring(7);
				comments.Add(comment.Id, comment);
				IHTMLElement ihtml;
				ihtml = GetByClassName(hcmt, "Ub gna");
				if (ihtml != null) comment.User = ihtml.innerText;
				ihtml = GetByClassName(hcmt, "Ct");
				if (ihtml != null) comment.Text = ihtml.innerText;
				ihtml = GetByClassName(hcmt, "o-U-s loLBj");
				if (ihtml != null)
				{
					HTMLAnchorElement a = ihtml as HTMLAnchorElement;
					int pos = a.href.IndexOf("lc=");
					if (pos >= 0) comment.ParentId = a.href.Substring(pos + 3);
				}
			}
			//привязка
			foreach (Comment cmt in comments.Values.ToArray())
			{
				if (string.IsNullOrEmpty(cmt.ParentId) || !comments.ContainsKey(cmt.ParentId)) continue;
				comments[cmt.ParentId].Childs.Add(cmt);
				comments.Remove(cmt.Id);
			}
			//рендерим
			string output = @"<html><head></head><body>";
			foreach (Comment cmt in comments.Values)
				output += Render(cmt);
			output += @"</body></html>";
			System.IO.File.WriteAllText(@"D:\ProjectsOrange\2014\YouTubeCommentsExtractor\WinApp\result.htm", output);
			this.Close();
		}
		private string Render(Comment comment)
		{
			string result = @"<div>";
			result += string.Format("<div style=\"font-weight: bold; margin-top: 10px;\">{0}</div>", comment.User);
			result += string.Format("<div>{0}</div>", comment.Text);
			if (comment.Childs.Count > 0)
			{
				result += "<div style=\"border-width:1px; border-color: black; border-left-style: solid; padding-left: 10px;\">";
				foreach (Comment cmt in comment.Childs)
					result += Render(cmt);
				result += @"</div>";
			}
			result += @"</div>";
			return result;
		}
		private IHTMLElement GetByClassName(HtmlElement container, string className)
		{
			foreach (HtmlElement element in container.All)
				if ((element.DomElement as IHTMLElement).className == className) return element.DomElement as IHTMLElement;
			return null;
		}
	}
	public class Comment
	{
		public string Id;
		public string User;
		public string Text;
		public string ParentId;
		public List<Comment> Childs = new List<Comment>();
	}
}
